#include "unitsearch.h"
#include "ui_unitsearch.h"

UnitSearch::UnitSearch(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::UnitSearch)
{
    ui->setupUi(this);

    treeRender();

}

UnitSearch::~UnitSearch()
{
    delete ui;
}
//显示函数
void UnitSearch::treeRender()
{
    treeItemRender(trUtf8("单位名称"),"unit","unitname");
     treeItemRender(trUtf8("单位属性"),"UnitNatrueCode","UnitNatrueName");
     treeItemRender(trUtf8("单位层次"),"UnitGradationCode","UnitGradationName");
     treeItemRender(trUtf8("单位性质"),"UnitCategoryCode","UnitCategory");
     treeItemRender(trUtf8("单位类别"),"UnitOfCategoryCode","UnitOfCategoryName");
     treeItemRender(trUtf8("单位级别"),"UnitLevelCode","UnitLevel");
     treeItemRender(trUtf8("单位隶属关系"),"AffiliationCode","AffiliationName");

}

void UnitSearch::treeItemRender(QString pitem,QString table,QString name )
{
    QTreeWidgetItem *pro=new QTreeWidgetItem(ui->all);
    pro->setText(0,pitem);
    QSqlQuery query;
    query.exec(QString("select %1 from %2").arg(name,table));
    while (query.next()) {
        QTreeWidgetItem *item=new QTreeWidgetItem(pro);
        item->setText(0,query.value(0).toString());

    }

}








//双击添加
void UnitSearch::on_all_itemDoubleClicked(QTreeWidgetItem *item, int column)
{


    if(item->parent())
    {
        QTreeWidgetItem *parent=new QTreeWidgetItem(ui->choose);
        parent->setText(column,item->parent()->text(0));
        QTreeWidgetItem *child=new QTreeWidgetItem(parent);
        child->setText(column,item->text(0));
        ui->choose->expandItem(parent);
    }

}
//双击删除
void UnitSearch::on_choose_itemDoubleClicked(QTreeWidgetItem *item, int column)
{
    if(item->parent())
    {
        delete item->parent();
    }
}
//确定添加
void UnitSearch::on_MakeSure_clicked()
{

    QString sqlAll;
    QSqlQuery queryAll;

    QSqlQuery qnatrue;//属性
    QString natrue;

    QSqlQuery qgra;//层次
    QString gradation;

    QSqlQuery qca;//性质
    QString ucatergory;

    QSqlQuery qoc;//类别
    QString uofcate;

    QSqlQuery qlev;//级别
    QString ulevel;

    QSqlQuery qaff;//隶属关系
    QString uaffilia;


      QTreeWidgetItemIterator it(ui->choose);//choose为已选条件列表
      QString unitname;//按单位名称查询
      QString unitname_1;//按条件参数查询
      while (*it) {
          if((*it)->text(0)=="单位名称")
          {

              unitname=(*it)->child(0)->text(0);

              emit sendData(unitname);

          }

          if((*it)->text(0)=="单位属性")
          {
              QString unatrueCode;
              qnatrue.exec(QString("select UnitNatrueId from UnitNatrueCode where UnitNatrueName = '%1'").arg((*it)->child(0)->text(0)));
              while(qnatrue.next())
              {
                  unatrueCode = qnatrue.value(0).toString();
                  //qDebug()<<nationCode;
              }
              natrue = QString(" and UnitNatrue = '%1'").arg(unatrueCode);

          }
          if((*it)->text(0)=="单位层次")
          {
              QString ugracode;
              qgra.exec(QString("select UnitGradationId from UnitGradationCode where UnitGradationName = '%1'").arg((*it)->child(0)->text(0)));
              while(qgra.next())
              {
                  ugracode = qgra.value(0).toString();
                  //qDebug()<<nationCode;
              }
              gradation = QString(" and UnitGradation = '%1'").arg(ugracode);

          }
          if((*it)->text(0)=="单位性质")
          {
              QString ucategorycode;
              qca.exec(QString("select UnitCategoryID from UnitCategoryCode where UnitCategory = '%1'").arg((*it)->child(0)->text(0)));
              while(qca.next())
              {
                  ucategorycode = qca.value(0).toString();
                  //qDebug()<<nationCode;
              }
              ucatergory = QString(" and Unitproperty = '%1'").arg(ucategorycode);

          }
          if((*it)->text(0)=="单位类别")
          {
              QString ucateofgorycode;
              qoc.exec(QString("select UnitOfCategoryId from UnitOfCategoryCode where UnitOfCategoryName = '%1'").arg((*it)->child(0)->text(0)));
              while(qoc.next())
              {
                  ucateofgorycode = qoc.value(0).toString();
                  //qDebug()<<nationCode;


              }
uofcate = QString(" and unitcategory = '%1'").arg(ucateofgorycode);

          }
          if((*it)->text(0)=="单位级别")
          {
              QString ulevelcode;
              qlev.exec(QString("select UnitLevelID from UnitLevelCode where UnitLevel = '%1'").arg((*it)->child(0)->text(0)));
              while(qlev.next())
              {
                  ulevelcode = qlev.value(0).toString();
                  //qDebug()<<nationCode;
              }
              ulevel = QString(" and unitlevel = '%1'").arg(ulevelcode);

          }

          if((*it)->text(0)=="单位隶属关系")
          {
              QString uaffiliacode;
              qaff.exec(QString("select AffiliationId from AffiliationCode where AffiliationName = '%1'").arg((*it)->child(0)->text(0)));
              while(qaff.next())
              {
                  uaffiliacode = qaff.value(0).toString();
                 // qDebug()<<uaffiliacode;
              }
              uaffilia = QString(" and Affiliation = '%1'").arg(uaffiliacode);

          }
     ++it;

}

//判断其他条件是否存在
      if (!natrue.isEmpty() or !gradation.isEmpty() or !ucatergory.isEmpty() or !uofcate.isEmpty() or !ulevel.isEmpty() or !uaffilia.isEmpty()) {


     sqlAll=QString("select unitname from unit where unitname!='%1'").arg(unitname);
     sqlAll=sqlAll+natrue+gradation+ucatergory+uofcate+ulevel+uaffilia;
     queryAll.exec(sqlAll);
     qDebug()<<sqlAll;
     while (queryAll.next()) {

         unitname_1=queryAll.value(0).toString();

         emit sendData(unitname_1);//向名册制作页面发送数据

     }
}

//
//    {
//        QString nationCode;
//        queryNation.exec(QString("select NationID from NationCode where Nation = '%1'").arg((*it)->child(0)->text(0)));
//        while(queryNation.next())
//        {
//            nationCode = queryNation.value(0).toString();
//            //qDebug()<<nationCode;
//        }
//        nation = QString(" and NationID = '%1'").arg(nationCode);

//    }
      QDialog::reject();

}



void UnitSearch::on_pushButton_clicked()
{
    QString coFilter = ui->tiaojian->text();
    QTreeWidgetItemIterator it(ui->all);
        while (*it)
        {
            if((*it)->text(0).contains(coFilter))
            {
                (*it)->setHidden(false);
                QTreeWidgetItem *item = *it;
                while (item->parent())
                {
                    item->parent()->setHidden(false);

                    item = item->parent();
                }
            }
            else
            {
                (*it)->setHidden(true);
            }
            ++it;
        }
}

void UnitSearch::on_pushButton_2_clicked()
{
    QString coFilter = ui->yixuan->text();
    QTreeWidgetItemIterator it(ui->choose);
        while (*it)
        {
            if((*it)->text(0).contains(coFilter))
            {
                (*it)->setHidden(false);
                QTreeWidgetItem *item = *it;

                while (item->parent())
                {
                    item->parent()->setHidden(false);

                    item = item->parent();
                }
            }
            else
            {
                (*it)->setHidden(true);
            }
            ++it;
        }
}

void UnitSearch::on_pushButton_3_clicked()
{
    QString coFilter = ui->UnitName->text();
    QTreeWidgetItemIterator it(ui->all);
        while (*it)
        {
            if((*it)->text(0).contains(coFilter))
            {
                (*it)->setHidden(false);
                QTreeWidgetItem *item = *it;
                while (item->parent())
                {
                    item->parent()->setHidden(false);

                    item = item->parent();
                }
            }
            else
            {
                (*it)->setHidden(true);
            }
            ++it;
        }
}
